﻿using System.ComponentModel.DataAnnotations;
using FluentValidationExample.Application.Products.ModelValidations;

namespace FluentValidationExample.Application.Products.Contracts;

public sealed record CreateProductRequest : IValidatableObject
{
    /// <summary>
    /// 当 AllowEmptyStrings = true 时，前端传 "" 可以通过验证
    /// 当 启用 <Nullable>enable</Nullable> 时，类型又没有声明为string?时,前端传null会类似于加上这个特性一样的效果： [Required(AllowEmptyStrings = true, ErrorMessage = "名称不能为空")]
    /// </summary>
    public string ProductName { get; set; } = string.Empty;


    [ProductLowerPrice(10)] public decimal Price { get; set; }


    public int Qty { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Qty <= 0)
        {
            yield return new ValidationResult("数量必须大于0", new[] { nameof(Qty) });
        }
    }
}